﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebInterface;
using WebReposity;

namespace WebServices
{
    public class IocUserService : IIocUserService
    {

        private List<IocUser> iocUsers = new List<IocUser>
        {
            new IocUser {Id=1,Age=11,Name="一",Phone="11111"},
            new IocUser {Id=2,Age=12,Name="二",Phone="22222"},
            new IocUser {Id=3,Age=13,Name="三",Phone="33333"},
            new IocUser {Id=4,Age=14,Name="四",Phone="44444"},
            new IocUser {Id=5,Age=15,Name="五",Phone="55555"},
        };

        public void AddOrUpdateUser(IocUser user)
        {
            var itemId = iocUsers.Where(i => i.Id == user.Id).Select(n => n.Id).FirstOrDefault();
            var item = iocUsers.Where(i => i.Id == user.Id).FirstOrDefault();
            if (item == null)
            {
                iocUsers.Add(user);
            }
            else
            {
                item.Id = user.Id;
                item.Age = user.Age;
                item.Name = user.Name;
                item.Phone = user.Phone;
            }
        }

        public void DeleteUser(int id)
        {
            var item = iocUsers.Where(i => i.Id == id).FirstOrDefault();
            if (item != null)
            {
                iocUsers.Remove(item);
            }
        }

        public IocUser GetUser(int id)
        {
            return iocUsers.Where(i => i.Id == id).FirstOrDefault();
        }

        public IEnumerable<IocUser> GetUsers()
        {
            return iocUsers;
        }
    }
}
